package com.letu.database.mybatis.typehandler;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * M9算法加密数据
 */
@Slf4j
public class TypeHandlerUtil {
    /**
     * 线上运行后勿修改，会影响已加密数据解密
     */
    private static final byte[] KEYS = "efc987654321camp".getBytes(StandardCharsets.UTF_8);

    public static final String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALX1mN9u2pZh6UzFmLwSN0QQAdybGeGRCHla/udRoHmctYjkn5zrS1IKMwRGIomjZDH4C4uRaQXSugP+s2MHTuzMgX/AOV39RZpfdPGNmxXnCHpXLueU/gI5nxyUEXNu7UzuveV616wlP0WrFQ6VDliq6/8F6KF5DJilnzhMuidTAgMBAAECgYBrolcGvD1DPbHSvwQFJEVa1pkzVPAvVSqNFr24ObnSlldwHcUo/qp0YiIhghdGEcfgIxk5Ow/D6Q4t9MlKZ/VP8HGf9jAEOtGNwis0ffy36Kex3pvKcPlllj+pOS+SqUo+iwJvaBPdPetLJC1nrAkufK7xyBN8229U0moVysV4wQJBAOCkJEAVKMU4ECzC0r9LGC9+p1jpD9tuf3xxHR3lHgkzcFghX/UHTVg3TYg8dkeIhB6ZraVbVMHjaE6RFJOWVqECQQDPXCmtj5KD20UPhoah4DGPGc+tH71l5QXk/1qXeDqv2E4fHsfRqszzGotwQYUSAzOvuhpWHda14OlGN38JwR1zAkA1V4SsGtolipx8pH56ohBKJt8CA8WaK3DmUc3CLYemU6qXosUwX92D1w34DbNgZRBKW9+Z5kgICkHYdibWrNpBAkAk9+VmUAZrA5iYX/94tKOhQ5TG5LW7iM04Ol5ISuMLnqCu46n0M2M+pCjf5o5226NHXQJ8/+zbmDF+lz5K2ucJAkBztOjeAXW3iEA7r7BZMhoQMhNN1YDIqJpNAoLU5teFnCzgQJZKNoPcK6oanV76TYWwl3fFvQPsVSH1LZzftEZ//";

    public static final String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC19ZjfbtqWYelMxZi8EjdEEAHcmxnhkQh5Wv7nUaB5nLWI5J+c60tSCjMERiKJo2Qx+AuLkWkF0roD/rNjB07szIF/wDld/UWaX3TxjZsV5wh6Vy7nlP4COZ8clBFzbu1M7r3letesJT9FqxUOlQ5Yquv/BeiheQyYpZ84TLonUwIDAQAB";

    public static void main(String[] args) {


            System.out.println(decrypt("6660d705d1cfb6207611fe31f6d6c08989d120c76e9dbecb34a98ac354dd1ea0"));

        System.out.println(decrypt("6660d705d1cfb6207611fe31f6d6c0894bc55ec9facd9c38a33a795ba54bb86c"));
        System.out.println(decrypt("19f90b49fd278670bac2caed7a6a6c1113456306877840ecc1ca77d0394652ce"));
    }
    public static String crypt(String value) {
        try {
            if (StringUtils.isNotBlank(value)) {
                AES aes = SecureUtil.aes(KEYS);
                String encrypt = aes.encryptHex(value);
                return encrypt;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return value;
        }
        return value;
    }

    public static String decrypt(String value) {
//        if (null == value) {
//            return null;
//        }
////        try {
////            Long l = System.currentTimeMillis();
////            String value2 = RSAEncrypt.decrypt(value, privateKey);
////            log.info("sm2 解密时间:{}", (System.currentTimeMillis() - l));
////            return value2;
////        } catch (Exception e) {
////            e.printStackTrace();
////            return value;
////        }

        try {
            return SecureUtil.aes(KEYS).decryptStr(value);
        } catch (Exception e) {
            e.printStackTrace();
            return value;
        }

//        return value;
    }

}
